package me.magicall.db.sql.expression;


/**
 * count函数
 * 
 * @author MaGiCalL
 */
public class Count extends FunctionExpression {

	private static final String COUNT1 = " COUNT(1) ";

	private boolean distinct;

	public Count() {
		super();
	}

	public Count(final boolean distinct) {
		super();
		this.distinct = distinct;
	}

	public Count(final Expression<?> expression) {
		super(expression);
	}

	public Count(final Expression<?> expression, final boolean distinct) {
		super(expression);
		setDistinct(distinct);
	}

	@Override
	public StringBuilder appendTo(final StringBuilder sb) {
		final Expression<?> expression = getExpression();
		if (expression == null) {
			sb.append(COUNT1);
		} else {
			sb.append(getName()).append("(");
			if (getDistinct()) {
				sb.append("DISTINCT ");
			}
			expression.appendTo(sb).append(")");
		}
		return sb;
	}

	public boolean getDistinct() {
		return distinct;
	}

	public void setDistinct(final boolean distinct) {
		this.distinct = distinct;
	}
}
